package com.kotiln.lgc.screenadapterlibrary.method1;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * @author $
 * @fileName UIUtils
 * @date2021/3/12 15:57
 * @describe TODO
 * @desc 用来按美工的基准值生成真实设备上需要的宽高值
 */
public class UIUtils {
    private volatile static UIUtils instance;

    public static UIUtils getInstance() {

        return instance;
    }

    public static UIUtils getInstance(Context context){
        if(instance==null){
            synchronized (UIUtils.class){
                if(instance==null){
                    instance=new UIUtils(context);
                }
            }
        }
        return instance;
    }

    //基准宽高
    public static final int STANDARD_WIDTH = 1080;
    //一般情况下系统状态栏的高度为48dp   //1920
    public static final int STANDARD_HEIGHT = 1872;
    //android系统源码下调用此代码获取当前系统状态栏的高度
    private static final String DIMEN_CLASS = "com.android.internal.R$dimen";

    //实际设备分辨率
    public float displayMetricsWidth;
    public float displayMetricsHeight;

    //初始化
    private UIUtils(Context context) {
        //获取屏幕的真实宽高
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        if (displayMetricsWidth == 0.0F || displayMetricsHeight == 0.0F) {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            //获取到状态栏的高度
            int systemBarHight = getSystemBarHeight(context);
            if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
                //横屏
                this.displayMetricsWidth = (float) displayMetrics.heightPixels;
                this.displayMetricsHeight = (float) displayMetrics.widthPixels - systemBarHight;
            } else {
                //竖屏
                this.displayMetricsWidth = (float) displayMetrics.widthPixels;
                this.displayMetricsHeight = (float) displayMetrics.heightPixels;
            }
        }
    }

    /**
     * @param context DIMEN_CLASS android源码文件下
     *                system_bar_height:此属性
     *                48:默认值
     * @return
     */
    private int getSystemBarHeight(Context context) {
        return getValue(context, DIMEN_CLASS, "system_bar_height", 48);
    }

    /**
     * @param context
     * @param attrGroupClass  安卓源码中存放围度的类
     * @param attrName:状态栏的信息
     * @param defValue:默认值
     * @return
     */
    private int getValue(Context context, String attrGroupClass, String attrName, int defValue) {
        try {
            Class e = Class.forName(attrGroupClass);
            Object o = e.newInstance();
            Field field = e.getField(attrName);
            //获取到的是一个ID
            int x = Integer.parseInt(field.get(o).toString());
            return context.getResources().getDimensionPixelOffset(x);
        } catch (Exception e) {
            return defValue;
        }
    }

    //开始获取缩放以后
    public float getWidth(float width) {
        return width*this.displayMetricsWidth/STANDARD_WIDTH;
    }

    public float getHeight(float height){
        return height*this.displayMetricsHeight/STANDARD_HEIGHT;
    }

    public int  getWidth(int width) {
        return (int)(width*this.displayMetricsWidth/STANDARD_WIDTH);
    }

    public int  getHeight(int height){
        return (int)(height*this.displayMetricsHeight/STANDARD_HEIGHT);
    }

}
